<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="cn.estore.entity.Product" %>
<%@ page import="cn.estore.dao.ProductDao" %>
<%@ page import="java.sql.SQLException" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>商品信息修改</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f8f9fa;
			margin: 0;
			padding: 0;
			text-align: center;
		}

		header {
			background-color: #343a40;
			color: white;
			padding: 20px 0;
		}

		header a {
			color: white;
			text-decoration: none;
			margin: 0 15px;
		}

		main {
			padding: 20px;
		}

		table {
			width: 80%;
			margin: 20px auto;
			border-collapse: collapse;
			box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
		}

		th, td {
			padding: 15px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}

		img {
			max-width: 100%;
			max-height: 150px;
			display: block;
			margin: 0 auto;
		}

		.product-info {
			padding-left: 20px;
		}

		input[type="text"] {
			width: 100%;
			padding: 8px;
			border: 1px solid #ccc;
			border-radius: 4px;
		}

		input[type="submit"] {
			background-color: #4CAF50;
			color: white;
			padding: 10px 20px;
			border: none;
			border-radius: 4px;
			cursor: pointer;
		}

		input[type="submit"]:hover {
			background-color: #45a049;
		}
	</style>
</head>
<body>
<header>
	<%
		// 检查用户是否登录，这里通过查找名为"username"的Cookie来简单判断（可根据实际登录逻辑完善）
		boolean isLoggedIn = false;
		Cookie[] cookies = request.getCookies();
		if (cookies!= null) {
			for (Cookie cookie : cookies) {
				if ("username".equals(cookie.getName())) {
					isLoggedIn = true;
					break;
				}
			}
		}

		if (isLoggedIn) {
	%>
	<a href="logout.jsp">退出登录</a>
	<%
	} else {
	%>
	<a href="login.jsp">登录</a>
	<%
		}
	%>
</header>
<main>
	<%
		request.setCharacterEncoding("UTF-8");
		int id;
		try {
			// 获取商品 id 参数并转换为整数，添加异常处理
			id = Integer.parseInt(request.getParameter("id"));
		} catch (NumberFormatException e) {
			out.println("<p>商品 id 参数错误，请检查输入。</p>");
			return;
		}

		if (isLoggedIn) {
			ProductDao dao = new ProductDao();
			List<Product> ProductList = null;
			// 调用数据库查询方法获取对应商品 id 的商品信息
			ProductList = dao.selectProductsById(id);

			if (ProductList!= null &&!ProductList.isEmpty()) {
				Product e = ProductList.get(0);
	%>
	<form action="UpdateProductByIdgo2.jsp" method="post">
		<table border="1" align="center">
			<tr>
				<td rowspan="6" width="40%">
					<img src="<%="productImages/" + e.getPicture()%>" alt="商品图片">
				</td>
				<td>
					<div>序号：<input type="text" name="id" value="<%=e.getId()%>"></div>
					<div>商品：<input type="text" name="name" value="<%=e.getName()%>"></div>
					<div>产地：<input type="text" name="producingArea" value="<%=e.getProducingArea()%>"></div>
					<div>单价：<input type="text" name="marketPrice" value="<%=e.getMarketPrice()%>">元</div>
					<div>简介：<input type="text" name="description" value="<%=e.getDescription()%>"></div>
					<%-- 如果登录，隐藏“登录才能购买”提示信息 --%>
					<div class="hide">登录才能购买</div>
					<input type="submit" value="提交修改">
				</td>
			</tr>
		</table>
	</form>
	<%
			} else {
				out.println("<p>未查询到对应商品信息。</p>");
			}
		} else {
			out.println("<p>您还未登录，请先登录后进行商品信息修改。</p><a href='login.jsp'>点击登录</a>");
		}
	%>
</main>
</body>
</html>
